<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	//个人中心收货地址主页面
    public function index()
    {
		$config = \DB::table("config")->get();
		$list = \DB::table("address")->where("uid",session("homeuser")->id)->get();
        return view("home/personal/myaddress",["list"=>$list,"config"=>$config]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		$status = $request->input("default_flg");
		$pName = $request->input("shouname");
		$street = $request->input("detailaddr");
		$post = $request->input("post");
		$phone = $request->input("phone");
		//得到最后一级的id
		$a = $request->input("address");
	
		//倒叙获取每一级的全部信息
		$a1 = \DB::table("district")->where("id",$a)->get();
		$a2 = \DB::table("district")->where("id",$a1[0]->upid)->get();
		$a3 = \DB::table("district")->where("id",$a2[0]->upid)->get();
		$a4 = \DB::table("district")->where("id",$a3[0]->upid)->get();
		
		//判断获取的倒叙最后一级是否为空
		if(empty($a4)){
			//将信息插入收货地址表
			if($status == 1){
				\DB::table("address")->where("uid",session("homeuser")->id)->update(["status"=>0]);
				\DB::table("address")->insert(["pName"=>$pName,"province"=>$a3[0]->name,"city"=>$a2[0]->name,"area"=>$a1[0]->name,"uid"=>session("homeuser")->id,"status"=>$status]);
			}else{
				\DB::table("address")->insert(["pName"=>$pName,"province"=>$a3[0]->name,"city"=>$a2[0]->name,"area"=>$a1[0]->name,"uid"=>session("homeuser")->id,"status"=>$status]);
			}
		}else{
			if($status == 1){
				\DB::table("address")->where("uid",session("homeuser")->id)->update(["status"=>0]);
				\DB::table("address")->insert(["pName"=>$pName,"province"=>$a4[0]->name,"city"=>$a3[0]->name,"area"=>$a2[0]->name,"street"=>$a1[0]->name,"post"=>$post,"phone"=>$phone,"uid"=>session("homeuser")->id,"status"=>$status]);
			}else{
				\DB::table("address")->insert(["pName"=>$pName,"province"=>$a4[0]->name,"city"=>$a3[0]->name,"area"=>$a2[0]->name,"street"=>$a1[0]->name,"post"=>$post,"phone"=>$phone,"uid"=>session("homeuser")->id,"status"=>$status]);
			}
			
		}
		//获取购物车uid
		$uid = session("homeuser")->id;
		$shoplist = \DB::table("shopcar")->where("uid",$uid)->where("status",1)->get();
		//获取该用户的收货地址(可能多条)
		$address = \DB::table("address")->where("uid",$uid)->get();
		$config = \DB::table("config")->get();
		return redirect('home/shop/jiesuan');
    }

	public function del($id)
    {
		//删除收获地址
		\DB::table("address")->where("id",$id)->delete();
		//return redirect('home/shop/jiesuan');
		return "Y";
    }
	
	//设置默认地址
    public function defaultaddr($id)
    {
		\DB::table("address")->where("uid",session("homeuser")->id)->update(["status"=>0]);
		\DB::table("address")->where("id",$id)->update(["status"=>1]);
		return "Y";
    }
    
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
